﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.Mobile.ContainerModel;
using System.Mobile.Mvc;
using TouchUI.System.Mobile.Mvc;
using LinkedService.Mobile;
using System.Resources;
using System.Reflection;
using Microsoft.Practices.ServiceLocation;
using LinkedService.Mobile.ServiceEntities;



namespace LinkedIn.Mobile
{
  /// <summary>
  /// Main entry point for the application. 
  /// Provides the linking between controllers and views
  /// </summary>
  public class ApplicationManager
  {
    private static Container container;
    private static LinkedInService service;

    static ApplicationManager()
    {
      Initialize();
    }

    /// <summary>
    /// Get configuration object
    /// </summary>
    public static IConfiguration Configuration
    {
      get
      {
        return ServiceLocator.Current.GetInstance<IConfiguration>();
      }
    }

    /// <summary>
    /// Create a static reference to our LinkedInService
    /// </summary>
    public static LinkedInService Service
    {
      get
      {
        if (service == null)
        {
          // Loading this can take a while
          service = new LinkedInService(new OAuthDataRepository());//("key","secret");
        }
        return service;
      }
    }  

    /// <summary>
    /// Where should we go in case of an error
    /// </summary>
    public static void NavigateToErrorHandling()
    {    
      MessageBox.Show(Service.LastError.Message);
      Cursor.Current = Cursors.Default;     
    }

    /// <summary>
    /// The last person retrieved from the service.
    /// </summary>
    public static Person LastPerson { get; set; }
    
    /// <summary>
    /// Where should we go in case of an error
    /// </summary>
    public static void NavigateToErrorHandling(string message)
    {
      NavigatorService.Navigator.Navigate<LoginController>(message);
    }
   

    /// <summary>
    /// Initialize the different controllers/views
    /// </summary>
    private static void Initialize()
    {
      // Set up the servicelocator
      BasicServiceLocator locator = new BasicServiceLocator();
      locator.AddComponent<IConfiguration, Configuration>(typeof(IConfiguration).ToString(), new Configuration());
      ServiceLocator.SetLocatorProvider(() => locator);

      container = new Container();
      container.Register<LoginForm>(c => new LoginForm());
      container.Register<LoginController>(c => new LoginController(c.Resolve<LoginForm>()))
            .InitializedBy((c, v) => v.Container = c);

      container.Register<ProfileDialog>(c => new ProfileDialog());
      container.Register<ProfileController>(c => new ProfileController(c.Resolve<ProfileDialog>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register main 
      container.Register<MainForm>(c => new MainForm());
      container.Register<MainFormController>(c => new MainFormController(c.Resolve<MainForm>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register connections
      container.Register<ConnectionsDialog>(c => new ConnectionsDialog());
      container.Register<ConnectionsController>(c => new ConnectionsController(c.Resolve<ConnectionsDialog>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register updates
      container.Register<UpdatesDialog>(c => new UpdatesDialog());
      container.Register<UpdatesController>(c => new UpdatesController(c.Resolve<UpdatesDialog>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register status
      container.Register<StatusView>(c => new StatusView());
      container.Register<StatusController>(c => new StatusController(c.Resolve<StatusView>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register Splash
      container.Register<NetworkPermissionView>(c => new NetworkPermissionView());
      container.Register<NetworkPermissionController>(c => new NetworkPermissionController(c.Resolve<NetworkPermissionView>()))
            .InitializedBy((c, v) => v.Container = c);

      // Register Search
      container.Register<SearchView>(c => new SearchView());
      container.Register<SearchController>(c => new SearchController(c.Resolve<SearchView>()))
            .InitializedBy((c, v) => v.Container = c);

      container.Register<InviteView>(c => new InviteView());
      container.Register<InviteController>(c => new InviteController(c.Resolve<InviteView>()))
            .InitializedBy((c, v) => v.Container = c);


      // Set ControllerProvider
      NavigatorService.SetControllerProvider(new ControllerProvider(container));


    }

    // Application entry point
    public static void Start()
    {
      // Move to the first control (MainForm)
      Controller startController = container.Resolve<MainFormController>();
      startController.Initialize();
      Application.Run(startController.View as Form);
    }
  }
}
